package com.xt.wikiperfumapi.Controller.brand;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.xt.wikiperfumapi.model.BrandInfo;
import com.xt.wikiperfumapi.model.BrandViewInfo;
import com.xt.wikiperfumapi.model.vo.BrandVo;
import com.xt.wikiperfumapi.model.vo.Label;
import com.xt.wikiperfumapi.model.vo.RestBean;
import com.xt.wikiperfumapi.model.wrapper.BrandWrapper;
import com.xt.wikiperfumapi.service.impl.BrandInfoServiceImpl;
import com.xt.wikiperfumapi.service.impl.BrandViewInfoServiceImpl;
import com.xt.wikiperfumapi.util.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RequestMapping("/api/brand")
@RestController
@Slf4j
public class BrandController {

    @Resource
    JwtUtil jwtUtil;

    @Resource
    BrandInfoServiceImpl brandInfoService;

    @Resource
    BrandWrapper brandWrapper;
    @Autowired
    private BrandViewInfoServiceImpl brandViewInfoServiceImpl;

    @RequestMapping("/get/{id}")
    public RestBean getById(@PathVariable long id, HttpServletRequest request){
        String uuid = jwtUtil.getJwtUUID(request);
        try {
            BrandInfo byId = brandInfoService.getById(id);
            BrandViewInfo brandViewInfo=new BrandViewInfo();
            brandViewInfo.setBrandId(id);
            brandViewInfo.setInputTime(new Date());
            brandViewInfoServiceImpl.save(brandViewInfo);//保存品牌浏览记录
            return RestBean.success(brandWrapper.brandToVo(byId,uuid));
        }catch (Exception e){
            log.info(e.getMessage());
            return RestBean.failure(400,"不存在该品牌");
        }
    }

    //品牌也需要分页
    @RequestMapping("/initList/{page}/{item}")
    public RestBean queryInitData(@PathVariable int page,@PathVariable int item,String name){
        PageMethod.startPage(page,item);
        List<BrandVo> brandVos=new ArrayList<>();
        List<BrandInfo> brandInfos = brandInfoService.list(new QueryWrapper<BrandInfo>()
                .eq("is_deleted", 0)
                .like("name", name)
        );
        brandInfos.forEach(el->{
            brandVos.add(brandWrapper.initDataVo(el));
            BrandViewInfo brandViewInfo=new BrandViewInfo();
            brandViewInfo.setBrandId(el.getBrandId());
            brandViewInfo.setInputTime(new Date());
            brandViewInfoServiceImpl.save(brandViewInfo);//保存品牌浏览记录
        });
        PageInfo<BrandVo> brandVoPageInfo = new PageInfo<>(brandVos);
        brandVoPageInfo.setTotal(brandInfoService.count(new QueryWrapper<BrandInfo>()
                .eq("is_deleted", 0)
                .like("name", name)
        ));
        return RestBean.success(brandVoPageInfo);
    }

    @RequestMapping("/initList")
    public RestBean initList(){
        List<BrandInfo> brandInfos = brandInfoService.list(new QueryWrapper<BrandInfo>()
                .eq("is_deleted", 0)
        );
        return RestBean.success(brandInfos);
    }

    @RequestMapping("/list/{page}/{item}")
    public RestBean list(@PathVariable int page,@PathVariable int item,String  name){
        PageMethod.startPage(page,item);
        List<BrandInfo> brandInfos = brandInfoService.list(new QueryWrapper<BrandInfo>()
                .eq("is_deleted",0)
                .like("name",name)
        );
        return RestBean.success(new PageInfo<>(brandInfos));
    }

    @RequestMapping("/update")
    public RestBean update(@RequestBody BrandInfo brandInfo){
        try {
            BrandInfo byId = brandInfoService.getById(brandInfo.getBrandId());
            byId.setUpdateTime(new Date());
            byId.setName(brandInfo.getName());
            return brandInfoService.updateById(byId)?
                    RestBean.success(byId):RestBean.failure(500,"server error");
        }catch (Exception e){
            return RestBean.failure(400,"不存在该品牌信息");
        }
    }
    @RequestMapping("/add")
    public RestBean add(@RequestBody BrandInfo brandInfo){
        long count = brandInfoService.count(new QueryWrapper<BrandInfo>()
                .eq("name", brandInfo.getName())
        );
        if(count > 0){
            return RestBean.failure(400,"已存在该品牌名称");
        }
        brandInfo.setBrandId((long)brandInfoService.list().size()+1);
        brandInfo.setInputTime(new Date());
        return brandInfoService.save(brandInfo)?RestBean.success(brandInfo):
                RestBean.failure(500,"server error");
    }

    @RequestMapping("/del/{id}")
    public RestBean del(@PathVariable long id){
        return brandInfoService.removeById(id)?
                RestBean.success():RestBean.failure(500,"server error");
    }

    @RequestMapping("/getId/{id}")
    public RestBean getById(@PathVariable long id){
        return RestBean.success(brandInfoService.getById(id));
    }

    @RequestMapping("/options")
    public RestBean options(){
        List<BrandInfo> brandInfos = brandInfoService.list(new QueryWrapper<BrandInfo>()
                .eq("is_deleted", 0)
        );
        List<Label> list=new ArrayList<>();
        brandInfos.forEach(el->{
            list.add(new Label(el.getName(),el.getBrandId().toString()));
        });
        return RestBean.success(list);
    }

}
